package com.traveling.hztraveling.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traveling.hztraveling.context.BaseContext;
import com.traveling.hztraveling.entity.po.HzCulture;
import com.traveling.hztraveling.entity.po.HzRoutes;
import com.traveling.hztraveling.entity.po.HzUser;
import com.traveling.hztraveling.mapper.HzRoutesMapper;
import com.traveling.hztraveling.service.IHzCultureService;
import com.traveling.hztraveling.service.IHzRoutesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.traveling.hztraveling.service.IHzUserService;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LHD
 * @since 2025-05-29
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class HzRoutesServiceImpl extends ServiceImpl<HzRoutesMapper, HzRoutes> implements IHzRoutesService {

    private final IHzUserService hzUserService;

    @Override
    @Transactional
    public int addRoutes(HzRoutes hzRoutes){
        // 要用BaseContext 获取用户ID
        Long userId = BaseContext.getCurrentId();
        if (userId==null){
            // 说明是管理端新增
            Integer id = hzRoutes.getUserId();
            HzUser one = hzUserService.getById(id);
            log.info("用户名:{}-----------------------------------------------------------------------------------------------",one.getUsername());
            hzRoutes.setUsername(one.getUsername());
        }
        else {
            hzRoutes.setUserId(Math.toIntExact(userId));
        }

        // 校验功能，不能重复添加
        List<HzRoutes> list = this.lambdaQuery()
                .eq(HzRoutes::getUserId, userId)
                .list();
         if (list!=null){
              for (HzRoutes hzRoutes1 : list) {
                 if (hzRoutes1.getRoute().equals(hzRoutes.getRoute()) && hzRoutes1.getDate().equals(hzRoutes.getDate())){
                     return -1;
                 }
             }
         }


        return baseMapper.insert(hzRoutes);
    }

    @Override
    public Page<HzRoutes> getOrders(Integer page, Integer pageSize){
        if (BaseContext.getCurrentId()==null){
            // 管理端查询全部
            Page<HzRoutes> page1 = new Page<>( page, pageSize);
            log.info("管理端查询全部:{}",this.lambdaQuery().page(page1));
            return this.lambdaQuery()
                    .page(page1);
        }
        Integer userId = Math.toIntExact(BaseContext.getCurrentId());
        Page<HzRoutes> page1 = new Page<>( page, pageSize);
        return this.lambdaQuery()
                .eq(HzRoutes::getUserId, userId)
                .page( page1);
    }

    @Override
    public Page<HzRoutes> getRouteOrders(int i, int i1, Object o, String keyword){
        Page<HzRoutes> page = new Page<>(i, i1);
        Integer userId = Math.toIntExact(BaseContext.getCurrentId());
        if (keyword.chars().anyMatch((Character::isDigit))){
            return this.lambdaQuery()
                    .eq(HzRoutes::getId, keyword)
                    .eq(HzRoutes::getUserId, userId)
                    .page(page);
        }else {
            String route1 = "三国文化之旅";
            String route2 = "山水风光之旅";
            if (route1.contains(keyword)){
                keyword = "culture";
            }
            if (route2.contains(keyword)){
                keyword = "nature";
            }
            return this.lambdaQuery()
                    .eq(HzRoutes::getUserId, userId)
                    .like(HzRoutes::getRoute, keyword)
                    .page(page);
        }
    }
}
